package com.example.caoqu.slice;

import com.example.caoqu.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.hiviewdfx.HiLog;

public class MainAbilitySlice extends AbilitySlice {
    private DirectionalLayout dependentLayoutMain;
    private DependentLayout dependentLayout;
    private DirectionalLayout directionalLayout;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        int widthPx = (int) (getContext().getResourceManager().getDeviceCapability().width * AttrHelper.getDensity(this));
        // 前景布局
        dependentLayout = (DependentLayout) findComponentById(ResourceTable.Id_dl_contacts);
        dependentLayout.setWidth(widthPx);
        // 菜单
        directionalLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_dtrl_contacts);
        // 主容器

        dependentLayoutMain = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_contacts_main);

        dependentLayoutMain.setWidth(widthPx + directionalLayout.getWidth());
    }

    @Override
    public void onActive() {





            // 设置水平拖动监听

            dependentLayout.setDraggedListener(Component.DRAG_HORIZONTAL, new Component.DraggedListener() {

                // 拖动动作开始X坐标

                private int startX = 0;

                // 拖动动作开始时的前景坐标，确定移动方向

                private float dependentLayoutStartX = 0;

                // 背景组件长度

                private int directionalLayoutWidth = 0;

                // 背景移动最远位置

                private int distance = 0;

                @Override

                public void onDragDown(Component component, DragInfo dragInfo) {
                }

                @Override

                public void onDragStart(Component component, DragInfo dragInfo) {
                    startX = dragInfo.startPoint.getPointXToInt();
                    dependentLayoutStartX = dependentLayoutMain.getContentPositionX();
                    directionalLayoutWidth = directionalLayout.getWidth();
                    distance = -directionalLayoutWidth;

                }



                @Override

                public void onDragUpdate(Component component, DragInfo dragInfo) {

                    // 拖动动作当前X坐标

                    int upX = dragInfo.updatePoint.getPointXToInt();

                    // 拖动距离

                    int upDataX = upX - startX;
                    // 前景子组件当时的位置

                    float dependentLayoutX = dependentLayoutMain.getContentPositionX();



                    // 预定位置

                    float position = dependentLayoutX + upDataX;



                    if (position < distance) {

                        position = distance;

                    }



                    if (position > 0) {

                        position = 0;

                    }



                    dependentLayoutMain.setContentPositionX(position);

                }



                @Override

                public void onDragEnd(Component component, DragInfo dragInfo) {
                    float nowX = dependentLayoutMain.getContentPositionX();



                    // 由于不是拖动，添加动画使移动流畅

                    AnimatorProperty animatorProperty = new AnimatorProperty();
                    animatorProperty
                            .setTarget(dependentLayoutMain)
                            .setLoopedCount(0)
                            .setDuration(200)
                            .setCurveType(Animator.CurveType.LINEAR);

                    // 设定以2/5为分界点

                    int divide = directionalLayoutWidth / 3;

                    if (dependentLayoutStartX == 0) {

                        // 从原点出发
                        if (nowX < -divide) {

                            animatorProperty.moveFromX(nowX).moveToX(distance);
                        } else {
                            animatorProperty.moveFromX(nowX).moveToX(0);
                        }
                    } else if (dependentLayoutStartX == distance) {
                        // 从隐藏点出发
                        if (distance - nowX > -divide) {
                            animatorProperty.moveFromX(nowX).moveToX(distance);
                        } else {
                            animatorProperty.moveFromX(nowX).moveToX(0);
                        }
                    }
                    animatorProperty.start();
                }
                @Override
                public void onDragCancel(Component component, DragInfo dragInfo) {

                    //HiLog.error(LABEL_LOG, "拖动取消");
                }

            });
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
